package com.hdyg.sixg.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.ui.CustomProgressDialog;
import com.hdyg.sixg.util.LogUtils;
import com.hdyg.sixg.util.ToastUtil;

import java.util.Map;

import butterknife.ButterKnife;

/**
 * Author
 * Time   2017/6/19
 * 当前类注释：所有fragment类的基类
 */

public abstract class BaseFragment<V, T extends BasePresenter<V>> extends Fragment {

    protected T mPresenter;
    protected Context mContext;
    protected Bundle mBundle;
    protected Map mMap;
    protected CustomProgressDialog progressDialog;
//    protected InputMethodManager inputMethodManager;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (view == null){
            view = View.inflate(getActivity(), getLayoutId(), null);
//            inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            ButterKnife.bind(this, view);
            mContext = getActivity();
            createPresenter();
            if (mPresenter != null) {
                mPresenter.attachView((V) this);
            }
            initView();
            initData();
        }
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        unbinder.unbind();
        if (mPresenter != null)
            mPresenter.detachView();
    }
//    protected void hideSoftKeyboard() {
//        if (getActivity().getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
//            if (getActivity().getCurrentFocus() != null)
//                inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
//                        InputMethodManager.HIDE_NOT_ALWAYS);
//        }
//    }

    protected abstract void createPresenter();

    protected abstract int getLayoutId();

    protected abstract void initView();

    protected abstract void initData();

    /**
     * 封装toast
     */
    public void toastNotifyShort(Object notify) {
        String str = null;
        if (notify instanceof String)
            str = notify.toString();
        else if (notify instanceof Integer)
            str = getResources().getString((Integer) notify);
        ToastUtil.show(str);
    }


    /**
     * activity跳转（无参数）
     *
     * @param className
     */

    public void startActivity(Class<?> className) {
        startActivity(className, null);
    }

    /**
     * activity跳转（有参数）
     *
     * @param className
     */
    public void startActivity(Class<?> className, Bundle bundle) {
        Intent mIntent = new Intent();
        mIntent.setClass(mContext, className);
        if (bundle != null) {
            mIntent.putExtras(bundle);
        }
        if (mContext.getPackageManager().resolveActivity(mIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            this.startActivity(mIntent);
        } else {
            LogUtils.e("activity not found for " + className.getSimpleName());
        }
    }

    /**
     * activity 跳转带回调[无参]
     *
     * @param className
     * @param requestCode
     */
    public void startActivityForResult(Class<?> className, int requestCode) {
        startActivityForResult(className, null, requestCode);
    }

    /**
     * activity 跳转带回调[有参]
     *
     * @param className
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> className, Bundle bundle,
                                       int requestCode) {
        Intent mIntent = new Intent();
        mIntent.setClass(mContext, className);
        if (bundle != null) {
            mIntent.putExtras(bundle);
        }
        if (mContext.getPackageManager().resolveActivity(mIntent, PackageManager.MATCH_DEFAULT_ONLY) != null) {
            startActivityForResult(mIntent, requestCode);
        } else {
            LogUtils.e("activity not found for " + className.getSimpleName());
        }
    }

}